<?php  if ( ! defined('__CORE_PATH__')) exit('No direct script access allowed');
/* SVN FILE: $Id: app_controller.php 25 2008-08-29 21:58:28Z namaless $ */
/**
 * Helper HTML.
 *
 * @author			Pereira Pulido Nuno Ricardo | Namaless | namaless@gmail.com
 * @copyright		Copyright 1981-2008, Namaless.
 * @link			http://www.namaless.com Namaless Blog
 * @version			1.0
 * @version			$Revision: 25 $
 * @modifiedby		$LastChangedBy: namaless $
 * @lastmodified	$Date: 2008-08-29 23:58:28 +0200 (ven, 29 ago 2008) $
 * @license			http://www.opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
* Classe principale.
*/
class Template
{
	var $_vars;

	/**
	 * Singleton Pattern.
	 *
	 * @var object
	 **/
	private static $instance = NULL;

	// --------------------------------------------------------------------

	/**
	 * Implemento il Singleton.
	 *
	 * @return	object
	 */
	public static function get_instance()
	{
		if ( self::$instance == NULL )
		{   
			$c = __CLASS__;
		    self::$instance =& new $c;
		}
		
		return self::$instance;
	}

	// --------------------------------------------------------------------

	private function __construct(){}
	
	// --------------------------------------------------------------------

	private function __clone(){}
	
	// --------------------------------------------------------------------

	public function __destruct(){}
	
	// --------------------------------------------------------------------

	/**
	 * Implemento il Factory.
	 */
	public static function factory(){}

	// --------------------------------------------------------------------

	public static function get_template_path($template_file)
	{
		return base_path("template/{$template_file}.phtml");
	}

	// --------------------------------------------------------------------

	public static function assign($var, $value)
	{
		self::get_instance()->_vars[$var] = $value;
	}

	// --------------------------------------------------------------------

	public static function load($template_file, $element = FALSE)
	{
		// Imposto la path della template.
		if ( $element )
		{
			$template_file = "elements/{$template_file}";
		}

		$template_path = self::get_template_path($template_file);
		
		// Controllo se esiste il template
		if ( file_exists($template_path) )
		{
			ob_start();
			extract(self::get_instance()->_vars, EXTR_SKIP);
			include (self::get_template_path($template_file));
			$output = ob_get_clean();
		}
		else
		{
			show_error(__("Template '{1}' not found.", $template_file));
		}
		
		return $output;
	}

	// --------------------------------------------------------------------

	public static function element($name, $data = array())
	{
		// Imposto la path della template.
		$template_file = "elements/{$name}";

		$template_path = self::get_template_path($template_file);
		
		// Controllo se esiste il template
		if ( file_exists($template_path) )
		{
			ob_start();
			extract($data, EXTR_SKIP);
			include (self::get_template_path($template_file));
			$output = ob_get_clean();
		}
		else
		{
			show_error(__("Template Element '{1}' not found.", $template_file));
		}
		
		return $output;
	}

	// --------------------------------------------------------------------

	function parse($template_file)
	{
		$output = self::load($template_file);
		
		Parser::parse($output);
	}
}